package com.itheima;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;
import java.util.List;

@SpringBootTest
public class test {

    @Autowired
    private EmpMapper empMapper;


    //selet id, name, phone, salary from emp where name like '%李%' and salar >= 5000
    @Test
    public void test(){

        QueryWrapper<Emp> qw = new QueryWrapper<Emp>();
        qw.like("name","李")
                .ge("salary",5000)
                .select("id", "name", "phone", "salary");

       List<Emp> list = empMapper.selectList(qw);
        System.out.println(list);
    }

    @Test
    public void test2(){
       // update emp set salar = 18000 where name = '李忠'
        UpdateWrapper<Emp> em = new UpdateWrapper<>();
        em.set("salary",18000)
                .set("update_time", LocalDateTime.now())
                .eq("name","李忠");
        empMapper.update(em);
    }
    @Test
    public void test3(){
        Emp emp = new Emp();
        emp.setCreateTime(LocalDateTime.now());
        emp.setSalary(18000);
        QueryWrapper<Emp> qw = new QueryWrapper<>();
        qw.eq("name","李忠");
        empMapper.update(emp,qw);
    }
    @Test
    public void test4(){
        // update emp set salary = salary +2000 where id in (5,6,7)
        UpdateWrapper<Emp> em = new UpdateWrapper<>();
        em.setSql("salary = salary +2000").in("id",5,6,7);
        empMapper.update(em);
    }
    @Test
    public void test5(){
        // update emp set salary = salary +2000 where id in (5,6,7)
        LambdaUpdateWrapper<Emp> em = new LambdaUpdateWrapper<>();
        em.setSql("salary = salary +2000").in(Emp::getId,5,6,7);
        empMapper.update(em);
    }

    @Test
    public void test6(){
        LambdaQueryWrapper<Emp> qw = new LambdaQueryWrapper<>();
        qw.like(Emp::getName,"李")
                .ge(Emp::getSalary,5000)
                .select(Emp::getId, Emp::getName, Emp::getPhone, Emp::getSalary);
        List<Emp> list = empMapper.selectList(qw);
        System.out.println(list);
    }
}
